/**     
 * @{#} ArithmeticFactory.java Create on 2011-7-22 上午11:36:25     
 *     
 * Copyright (c) 2011 by xiaofeng.zhouxf     
 */
package com.sefer.memcached.arithmetic;

import java.util.HashMap;
import java.util.Map;

import com.sefer.memcached.constants.HashPolicy;

/**
 * ArithmeticFactory
 * 
 * @author <a href="mailto:xiaofeng.zhouxf83@gmail.com">xiaofeng.zhouxf</a>
 * @version 1.0
 */
public class ArithmeticFactory {
	private final static Map<String, HashArithmetic> arithmeticMap = new HashMap<String, HashArithmetic>();
	static {
		arithmeticMap.put(HashPolicy.MOD_HASH.getPolicy(),
				new ModHashArithmetic());
		arithmeticMap.put(HashPolicy.CONSISTENCY_HASH.getPolicy(),
				new ModHashArithmetic());
	}

	/**
	 * getArithmetic
	 * 
	 * @param policy
	 *            policy for arithmetic
	 * @see HashPolicy
	 * @return HashArithmetic
	 */
	public static HashArithmetic getArithmetic(HashPolicy policy) {
		HashArithmetic hashArithmetic = arithmeticMap.get(policy.getPolicy());
		if (hashArithmetic == null) {
			return arithmeticMap.get(HashPolicy.MOD_HASH.getPolicy());
		}

		return hashArithmetic;
	}
}
